<template>
  <div class="search">
    <el-autocomplete clearable placeholder="请输入医院名称" />
    <el-button type="primary" size="default" :icon="Search" v-model="hosname" :fetch-suggestions="fetchData"
      :trigger-on-focus="false" @select="goDetail">搜索</el-button>
  </div>
</template>

<script setup lang="ts" name="Search">
//引入UI组件库提供的图标
import { Search } from '@element-plus/icons-vue'
import { ref } from 'vue'
//引入api请求方法、TS类型限制
//@ts-ignore
import { reqHospitalInfo } from '@/api/home/index'
//@ts-ignore
import { HospitalInfo } from '@/api/home/type'
//引入路由器
import { useRouter } from 'vue-router'

//存储搜索的关键字：医院名称
let hosname = ref<string>('')
//创建路由器对象
let $router = useRouter()

// 顶部组件的回调：当用户输入完关键字函数会发送一次请求得到数据，整理数据变成可以展示的数据格式，调用回调展示推荐项
const fetchData = async (keyword: string, cb: any) => {
  let result: HospitalInfo = await reqHospitalInfo(keyword)
  let showData = result.data.map(item => {
    return {
      value: item.hosname,
      hoscode: item.hoscode
    }
  })
  cb(showData)
}
//点击某个推荐项：跳转到医院详情页
const goDetail = (item: any) => {
  $router.push({
    path: '/hospital/register',
    query: {
      hoscode: item.hascode
    }
  })
}
</script>

<style scoped lang="scss">
.search {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0;

  // :deep(.el-input__wrapper){
  //   width: 600px;
  //   margin-right: 10px;
  // }
  :deep(.el-autocomplete) {
    width: 600px;
    margin-right: 10px;
  }
}
</style>